package org.example.blog.service;

import lombok.extern.slf4j.Slf4j;
import org.example.blog.mapper.BlogMapper;
import org.example.blog.mapper.UserInfoMapper;
import org.example.blog.model.BlogInfo;
import org.example.blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogMapper blogMapper;
    public  UserInfo selectByName(String userName) {
        return userInfoMapper.selectByName(userName);

    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //1. 根据博客ID, 获取作者ID
        //2. 根据作者ID, 获取作者信息
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if (blogInfo==null && blogInfo.getUserId()<=0){
            log.error("图书不存在或图书作者信息不合法, blogId:{}", blogId);
            return null;
        }
        return userInfoMapper.selectById(blogInfo.getUserId());
    }
}
